/**
 * Get options for given element and templates.
 *
 * @param {dj.model.Base} element
 * @param {ElementTemplates} elementTemplates
 * @param {Translate} translate
 */
import { Key } from 'react';
import { find } from 'lodash-es';

class Option {
  id: Key;

  name: string;

  value: any;

  constructor(id: Key, name: string, value: any, translate?: any) {
    this.id = id;
    this.name = translate(name) || name;
    this.value = value;
  }
}

export function getOptions(list: any[], translate: any): Option[] {
  return list.map((item: any) => new Option(item.id, item.name, item.value, translate));
}

export function getOption(optionId: Key, options: Option[]): Option {
  return find(options, (option) => {
    return optionId === option.value;
  });
}
